// pages/zstz/category/category.js
const app = getApp().globalData;
var md5 = require("../../../utils/md5.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeId: '',
    typeName: '',
    userInfo: null,
    singleAnswerNumber: '',
    singleAnswerTime: '',
    singleScore: '',
    answerCountScore: '',
    alreadyAnswerCount: '',
    totalScore: '',
    hasShowRank: '',
    hasNavigateTo: true, //防止多次点击，打开多个页面
    description: '',
    activitieType: [{
      colour: "blue",
      name: "线上竞答",
      explain: "自我修炼",
      bindtap: "goAnswer"
    }, 
    // {
    //   colour: "green",
    //   name: "好友Pk",
    //   explain: "邀请微信好友",
    //   bindtap: "friendPk"
    // }, {
    //   colour: "purple",
    //   name: "单位对战",
    //   explain: "在线匹配本单位好友",
    //   bindtap: "unitPk"
    // }, 
    {
      colour: "red",
      name: "排行榜",
      explain: "",
      bindtap: "goRank"
    }],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUniQuestionType(options.typeId); //根据分类id获取题库类型，判断是否符合参与条件
    this.setData({
      typeId: options.typeId,
      typeName: options.typeName,
      userInfo: wx.getStorageSync('userInfo')
    });
    wx.setNavigationBarTitle({ //动态修改标题
      title: options.typeName
    });
  },

  //根据分类id获取题库类型，判断是否符合参与条件
  getUniQuestionType: function (typeId) {
    let that = this;
    var datetime = new Date().Format('yyyy-MM-dd hh:mm:ss');
    var token = md5.md5("[typeId=" + typeId + "][openId=" + wx.getStorageSync('openid') + "][pkPattern=线上竞答][date=" + datetime + "]zs0dj4jc8dwtz").toLowerCase();
    wx.request({
      url: 'https://zstz.chaoxing.com/external/interface/wx/getUniQuestionType?token=' + token + '&typeId=' + typeId + '&openId=' + wx.getStorageSync('openid') + '&pkPattern=线上竞答',
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({
            singleAnswerNumber: res.data.data.singleAnswerNumber, //单次答题数量
            singleAnswerTime: res.data.data.singleAnswerTime, //单次答题时间
            singleScore: res.data.data.singleScore, //单题分值
            answerCountScore: res.data.data.answerCountScore, //限制每日答题次数或答题得分
            alreadyAnswerCount: res.data.data.alreadyAnswerCount, //今日已经答题次数
            totalScore: res.data.data.totalScore, //此次答题总得分
            hasShowRank: res.data.data.hasShowRank, //是否显示排行榜(0显示,1隐藏)
            description: res.data.data.description //活动介绍
          })
        }
      },
      fail: function () {
        wx.showToast({ //弹窗
          title: '根据分类id获取题库异常！',
          icon: 'none'
        })
      }
    })
  },

  //点击'线上竞答'
  goAnswer: function () {
    let that = this;
    if (that.data.answerCountScore != '') { //防止接口慢，用户点击快，跳过校验
      //1.判断此次答题总得分是否超过后台设置限制
      if ((that.data.answerCountScore == 60 && that.data.totalScore > 60) || (that.data.answerCountScore == 80 && that.data.totalScore > 80)) {
        wx.showToast({ //弹窗
          title: '最高得分超过' + that.data.answerCountScore + '分,不能再答题了,快去领奖吧!',
          icon: 'none'
        })
        //2.判断今日答题次数是否超过后台限制次数
      } else if (that.data.alreadyAnswerCount >= that.data.answerCountScore) {
        wx.showToast({ //弹窗
          title: '每天只能参与' + that.data.answerCountScore + '次,您今天已经参与' + that.data.answerCountScore + '次,明天再来吧!',
          icon: 'none'
        })
      } else {
        //3.开始答题
        if (that.data.hasNavigateTo) {
          wx.navigateTo({
            url: '../selfPkExam/selfPkExam?typeId=' + that.data.typeId + '&typeName=' + that.data.typeName + '&singleAnswerNumber=' + that.data.singleAnswerNumber + '&singleAnswerTime=' + that.data.singleAnswerTime + '&singleScore=' + that.data.singleScore
          })
          that.setData({
            hasNavigateTo: false
          })
        }
      }
    }
  },

  //排行榜
  goRank: function () {
    wx.navigateTo({
      url: '../activityRank/activityRank?typeId=' + this.data.typeId
    })
  },

  //好友pk
  friendPk: function () {
    wx.showToast({ //弹窗
      title: '暂未开启!',
      icon: 'none'
    })
  },

  //单位对战
  unitPk: function () {
    wx.showToast({ //弹窗
      title: '暂未开启!',
      icon: 'none'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUniQuestionType(this.data.typeId); //根据分类id获取题库类型，判断是否符合参与条件
    this.setData({
      hasNavigateTo: true
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      alreadyAnswerCount: '',
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.setData({
      alreadyAnswerCount: '',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})